package cn.iocoder.yudao.framework.crypto.pojo;

import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.crypto.symmetric.AES;
import cn.iocoder.yudao.framework.crypto.annotation.ApiRequestDecrypt;
import cn.iocoder.yudao.framework.crypto.annotation.ApiResponseEncrypt;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 *  RSA+AES 加密解密配置
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/03/24
 */
@ConfigurationProperties(prefix = "xianmin.crypto")
@Data
public class CryptoProperties {
    /**
     * 是否开启加密
     *
     * @see ApiResponseEncrypt
     * @see ApiRequestDecrypt
     */
    private boolean enable = false;
    /**
     * RSA 私钥
     */
    private String privateKey;
    /**
     * RSA 公钥
     */
    private String publicKey;
    /**
     * 客户端传递给服务端的 AES 秘钥的请求头
     */
    private String aesHeader = "X-Secret-Key";
    /**
     * 是否打印日志
     */
    private boolean showLog = true;

    public static void main(String[] args) {
        AES aes = SecureUtil.aes();
        RSA rsa = SecureUtil.rsa("""
                MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAMEztiWnXxVvKji62N8kMCgZ6ypBr1mWG/Tid0dJElHnhP36CqZm1FaGS04GRGcH1VegHCovsW1k2YyNeLzBwoCJmz1kose10I0RGy6qM3gPriDTrxBXo4vTfdc8NhUkpqVHjcTQzq7vwlLDXdcH91jkTHfzz1H6vsUoBHF+zSMFAgMBAAECgYANRxiFjfMi7fjg3TZ6X2pP1zgqN0ltxrs1ITtBUuxHZLnnGoAQ35BIQgJ64kNhMVCgLQjCfXScKAordq2Ni2NqgzLOVrNaqgts00PRCUNKOY4in+EWKhPWTQ3FH5iTA/U70ahPkDaRuKOhfKYR9MTvrQabI+3TigzIGInLkGd6AQJBAP48PUF5EUlQdNk9gurr8H/b4n4VhFfxRtHZMz+5aPRM16uKPx4sbQIdbwICJRnuJQWeGOVqlh61KSL52FSCCDECQQDCiwUNsgwOfBEEcpH+sUcsOFCisGS21FnfTwgr6EorVbk17LevjpheG3KEc9+xn3U8csuWS8FSsqNyhK5pUycVAkBeoHtISFCn0ht71EpLOEBOxr/5n/afaP2sE8TgfhlTfNp1TcKKb4yvUddEjxnSGfKLsbmggliX22QRUZ80bSWxAkAB7fMM4bkw45YfnytHhWaZT62tD2bQUWbayiPgm5swttbxa/meaiGkvUzzd1q+nkBBazoNsY6nTVJFrn80WDjJAkAIHM6qgWi7Y5j4gTvMVr8F1b+qja8DcushvMfhHVyioDSTD1/dKo0H6WIrSGHLkRCXNr6vHXFIHtGzd7fUmo8d
                """, """
                MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDBM7Ylp18Vbyo4utjfJDAoGesqQa9Zlhv04ndHSRJR54T9+gqmZtRWhktOBkRnB9VXoBwqL7FtZNmMjXi8wcKAiZs9ZKLHtdCNERsuqjN4D64g068QV6OL033XPDYVJKalR43E0M6u78JSw13XB/dY5Ex3889R+r7FKARxfs0jBQIDAQAB
                """);
        System.out.println("RSA 私钥：" + rsa.getPrivateKeyBase64());
        System.out.println("RSA 公钥：" + rsa.getPublicKeyBase64());
        byte[] aesKeyPlain = aes.getSecretKey().getEncoded();
        System.out.println("AES 秘钥原始：" + HexUtil.encodeHexStr(aesKeyPlain));
        String aesKeyEncrypted = rsa.encryptBase64(aesKeyPlain, KeyType.PublicKey);
        System.out.println("AES 秘钥加密：" + aesKeyEncrypted);
        byte[] aesKeyDecrypted = rsa.decrypt(aesKeyEncrypted, KeyType.PrivateKey);
        System.out.println("AES 秘钥解密：" + HexUtil.encodeHexStr(aesKeyDecrypted));
    }

}
